package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.DynamicModel;
import one.stand.model.ResultModel;
import one.stand.model.essay.DynamicCompanyModel;
import one.stand.model.essay.TaskCustomerModel;
import one.stand.model.essay.VisitorsDetailModel;
import one.stand.request.essay.CompanyDynamicsQuery;
import one.stand.request.essay.DynamicQuery;
import one.stand.request.essay.VisitorsDetailQuery;
import one.stand.response.DynamicResponse;
import one.stand.response.essay.CompanyGoodsResponse;
import one.stand.response.essay.DynamicDetailResponse;
import one.stand.response.essay.DynamicModelResponse;
import one.stand.response.essay.VisitorsResponse;
import one.stand.service.BaseService;
import one.stand.service.IDynamicService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 文章查询
 * @author luoyang
 * @date 2020/5/8 18:27
 */
@Service
public class DynamicServiceImpl extends BaseService implements IDynamicService {

    @Override
    public ResultModel<PageInfo<DynamicModelResponse>> queryDynamicData(DynamicQuery request) {
        if(request.getCompanyId() == null){
            request.setCompanyId(request.getCId());
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        log.info("queryDynamicData:{}",request);
        List<DynamicCompanyModel> dynamicModels = this.dynamicMapper.queryDynamicData(request);

        List<DynamicModelResponse> dynamicResponses = new ArrayList<>();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        dynamicModels.forEach(dynamicModel -> {
            DynamicModelResponse dynamicResponse = new DynamicModelResponse();
            BeanUtils.copyProperties(dynamicModel,dynamicResponse);
            //检查文章是否被分享过
            TaskCustomerModel taskCustomerModel = this.taskCustomerMapper.queryByDynamicId(dynamicModel.getDynamicId());
            dynamicResponse.setTask(Optional.ofNullable(taskCustomerModel).isPresent());
            dynamicResponse.setTaskCustomerId(Optional.ofNullable(taskCustomerModel).isPresent() ? taskCustomerModel.getId() : 0);
            dynamicResponse.setDetails("");

            dynamicResponse.setCreateTime(formatter.format(dynamicModel.getCreateTime()));
            dynamicResponses.add(dynamicResponse);
        });

        PageInfo<DynamicModelResponse> pageInfo = new PageInfo(dynamicResponses);
        return ResultModel.success(pageInfo);
    }

    /**
     * 查询文章访客详情
     * @param request
     * @return
     */
    @Override
    public ResultModel<DynamicModelResponse> queryDynamicVisitors(VisitorsDetailQuery request) {

        DynamicModelResponse response = new DynamicModelResponse();

        DynamicCompanyModel dynamicCompanyModel = this.dynamicMapper.queryDynamicVisitors(request);
        if(dynamicCompanyModel == null){
            return ResultModel.fail();
        }
        List<VisitorsDetailModel> visitorsDetailModels = this.essayVisitorsDataMapper.queryDynamicVisitorsDetail(request);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<VisitorsResponse> visitorsResponses = new ArrayList<>();
        if(visitorsDetailModels != null){
            visitorsDetailModels.forEach(visitorsDetailModel -> {
                if(visitorsDetailModel != null){
                    VisitorsResponse visitorsResponse = new VisitorsResponse();
                    BeanUtils.copyProperties(visitorsDetailModel,visitorsResponse);
                    visitorsResponses.add(visitorsResponse);
                }
            });
        }
        BeanUtils.copyProperties(dynamicCompanyModel,response);
        response.setCreateTime(formatter.format(dynamicCompanyModel.getCreateTime()));

        //访客数据
        response.setCommentModelList(visitorsResponses);

        return ResultModel.success(response);
    }

    /**
     * 查询公司下的文章
     * @param request
     * @return
     */
    @Override
    public ResultModel<PageInfo<DynamicModelResponse>> queryCompanyDynamics(CompanyDynamicsQuery request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<DynamicModelResponse> dynamicModelResponses = this.dynamicMapper.queryCompanyDynamics(request.getCompanyId());
        if(dynamicModelResponses == null ){
            return ResultModel.noData();
        }
        PageInfo<DynamicModelResponse> pageInfo = new PageInfo(dynamicModelResponses);
        return ResultModel.success(pageInfo);
    }

    /**
     * 查询文章详情 包含关联的图片和商品
     * @param request
     * @return
     */
    @Override
    public ResultModel<DynamicDetailResponse> queryCompanyDynamicById(CompanyDynamicsQuery request) {
        DynamicModel dynamicModel = this.dynamicMapper.selectByPrimaryKey(request.getDynamicId());
        if(dynamicModel == null){
            return ResultModel.noData();
        }
        DynamicDetailResponse modelResponse = new DynamicDetailResponse();

        BeanUtils.copyProperties(dynamicModel,modelResponse);
        List<CompanyGoodsResponse> companyGoodsResponses = this.essayRelatedMapper.queryCompanyGoods(request.getDynamicId());
        //商品
        modelResponse.setCompanyGoodsResponses(CollectionUtils.isEmpty(companyGoodsResponses) ? new ArrayList<>(): companyGoodsResponses);

        List<DynamicModelResponse> dynamicModelResponses = this.essayRelatedMapper.queryCompanyDynamic(request.getDynamicId());
        //文章
        modelResponse.setDynamicDetailResponses(CollectionUtils.isEmpty(dynamicModelResponses) ? new ArrayList<>(): dynamicModelResponses);

        return ResultModel.success(modelResponse);
    }

    @Override
    @Transactional
    public ResultModel<Boolean> deleteByDynamicId(CompanyDynamicsQuery request) {
        if(this.essayStaffDataMapper.deleteByDynamicId(request.getDynamicId()) > 0){
            return ResultModel.success();
        }
        return ResultModel.fail("文章已删除");
    }

    @Override
    public DynamicDetailResponse queryById(String dynamicId) {
        DynamicDetailResponse response = new DynamicDetailResponse();
        DynamicModel dynamicModel = this.dynamicMapper.selectByPrimaryKey(Integer.valueOf(dynamicId));
        if(dynamicModel != null){
            BeanUtils.copyProperties(dynamicModel,response);
        }
        return response;
    }

    @Override
    public void updateShareNum(Integer dynamicId){
        //更新动态的分享记录
        DynamicModel dynamicModel = this.dynamicMapper.selectByPrimaryKey(dynamicId);
        if(dynamicModel != null){
            dynamicModel.setShareNum( dynamicModel.getShareNum() == null ? 1 : dynamicModel.getShareNum() + 1);
            this.dynamicMapper.updateShareNum(dynamicModel);
        }
    }

    @Override
    @Transactional
    public void updateRead(Integer dynamicId,Integer readNum,Integer readTime) {
        DynamicModel dynamicModel = this.dynamicMapper.selectByPrimaryKey(dynamicId);
        if(dynamicModel != null){
            dynamicModel.setReadTime( dynamicModel.getReadTime() == null ? readTime : dynamicModel.getReadTime() + readTime);
            dynamicModel.setReadNum( dynamicModel.getReadNum() == null ? readNum : dynamicModel.getReadNum() + readNum);
            this.dynamicMapper.updateByPrimaryKeySelective(dynamicModel);
        }
    }
}
